// Copyright (c) Microsoft Corporation. All rights reserved.  
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;

// TODO-johnls-1/17/2008: Ideally this program would start up through a native shime that would host the CLR.  See 
// http://msdn.microsoft.com/msdnmag/issues/06/08/CLRInsideOut/default.aspx for info. on how to do this.
// The reason is that then we can use the version of csr.exe/csr.exe.config that is actually in the PATH and 
// not the one that is in the registry.

namespace Microsoft.Tools.CodeRunner
{
    public class Program
    {
        [LoaderOptimization(LoaderOptimization.MultiDomain)]
        [STAThread()]
        public static int Main(string[] args)
        {
            #if DEBUG
            // This is useful in debugging unit tests that run an instance of csr.exe
            if (File.Exists("csr.exe.breakonentry.flag"))
                Debugger.Break();
            #endif

            // TODO-johnls-11/25/2007: The task will usually run in another domain inside MSBuild - should
            // we always run it that way here?

            Csr csr = new Csr(new ConsoleOutputter());

            // Get any settings from the .config file
            if (!((IProcessConfiguration)csr).ProcessConfiguration(ConfigurationUserLevel.None))
            {
                return 1;
            }

            // Get any settings from the environment
            if (!((IProcessEnvironment)csr).ProcessEnvironment())
            {
                return 1;
            }

            // Get all the command line arguments
            if (!((IProcessCommandLine)csr).ProcessCommandLine(args))
            {
                return 1;
            }

            try
            {
                csr.Execute();
            }
            catch (Exception e)
            {
                // Log any exceptions that slip through
                csr.Output.Error(e.ToString());
            }

            return csr.ExitCode;
        }
    }
}
